package com.langsheng.lsintell.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.langsheng.lsintell.R;
import com.langsheng.lsintell.cmd.request.LSRequest;
import com.langsheng.lsintell.config.LSLoginInfos;
import com.langsheng.lsintell.data.LSGetStoreListReq;
import com.langsheng.lsintell.data.LSGetStoreListRes;
import com.langsheng.lsintell.interfaces.LSResDataListener;
import com.langsheng.lsintell.network.LSNet;
import com.langsheng.lsintell.utils.LSUtil;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LSStoreFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LSStoreFragment extends LSBaseFragment {

    private PullToRefreshListView listView;
    private ListAdapter adapter;
    private List<LSGetStoreListRes.RecordsBean.MBean> dataList;

    public LSStoreFragment() {
        // Required empty public constructor
    }

    public static LSStoreFragment newInstance() {
        LSStoreFragment fragment = new LSStoreFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lsstore, container, false);
        listView = (PullToRefreshListView) view.findViewById(R.id.store_list_view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new ListAdapter(getContext());
        listView.setAdapter(adapter);
        doFetchStoreList("0");
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LSUtil.showToast(getContext(), "pos = " + position);
            }
        });
    }

    private void doFetchStoreList(final String createData) {
        if (LSLoginInfos.getOurInstance().getLoginData() == null) {
            return;
        }

        LSRequest request = new LSRequest() {
            @Override
            public void createCmd() {
                setCmdCode(0x2025);
                LSGetStoreListReq req = new LSGetStoreListReq();
                req.setToken(LSLoginInfos.getOurInstance().getLoginData().getToken());
                req.setCreatedate(createData);
                req.setPagecount("6");
                setContent(JSONObject.toJSON(req).toString());
            }
        };
        LSNet.getInstance().sendRequest(request, true, new LSResDataListener() {
            @Override
            public boolean onResponse(String content) {
                LSGetStoreListRes res = JSONObject.parseObject(content, LSGetStoreListRes.class);
                dataList = res.getRecords().getM();
                adapter.notifyDataSetChanged();
                return false;
            }

            @Override
            public boolean onResponseErr(String errCode, String content) {
                return false;
            }
        });
    }

    private class ListAdapter extends BaseAdapter {

        class ViewHolder {
            ImageView itemIcon;
            TextView itemDesc;

            ViewHolder(View view) {
                itemIcon = (ImageView) view.findViewById(R.id.iv_store_icon);
                itemDesc = (TextView) view.findViewById(R.id.tv_store_desc);
            }
        }

        private Context context;

        public ListAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            if (dataList == null) {
                return 0;
            }
            return dataList.size();
        }

        @Override
        public LSGetStoreListRes.RecordsBean.MBean getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.view_store_item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            LSGetStoreListRes.RecordsBean.MBean data = getItem(position);
            holder.itemDesc.setText(data.getS4());
            return convertView;
        }
    }


}
